package com.huayue.point.advice;

import com.huayue.point.bean.ResultBean;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import java.util.Map;

@ControllerAdvice
public class ResultAdvice  implements ResponseBodyAdvice {

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        ResultBean resultBean = new ResultBean();
        if (methodParameter.getMethod().getName().equals("error")) {
            resultBean.setStatus(0);
            resultBean.setMessage(((Map<String,String>)o).get("message"));
        } else {
            resultBean.setStatus(1);
            resultBean.setData(o);
        }
        return resultBean;
    }

    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        return true;
    }
}